package org.lee;

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;
import org.lee.img.IRecognizeMainColor;
import org.lee.img.impl.RecognizeMainColor;
import org.lee.pdf.IPdfExporter;
import org.lee.pdf.impl.PdfExporter;
import org.lee.pdf.model.ApiResult;
import org.lee.pdf.model.ResultCode;
import org.lee.pdf.utils.FileUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Scanner;

/**
 * @author jackielee
 * @version 1.0
 * @description App
 * @date 2021/5/7
 */
public class App {
  public static void main1(String[] args) {
    IPdfExporter exporter = new PdfExporter();
    System.out.println("please input the pdf file:");
    String pdfPath = new Scanner(System.in).nextLine();
    String pathSave =
        pdfPath.substring(0, pdfPath.lastIndexOf(File.separator)) + File.separator + "export.docx";
    System.out.println("save to the path:" + pathSave);
    ApiResult result = exporter.export(pdfPath, pathSave);
    if (result.getCode() != ResultCode.SUCCESS.getCode()) {
      System.out.println(result.getMessage());
      return;
    }

    System.out.println("end.");
  }

  public static void main2(String[] args) {
    String srcPdf = "C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.pdf";
    String targetDoc = "C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.docx";
    Document doc = new Document(srcPdf);
    doc.save(targetDoc, SaveFormat.DocX);
  }

  public static void main(String[] args) {
    String srcPdf = "C:\\Users\\Jackie\\Downloads\\dd\\all2Html\\Linux就该这么学.pdf";
    String targetDoc = "C:\\Users\\Jackie\\Downloads\\dd\\all2Html\\Linux就该这么学.";
    PdfDocument doc = new PdfDocument();
    doc.loadFromFile(srcPdf);
    doc.saveToFile(targetDoc + "docx", FileFormat.DOCX);
    doc.saveToFile(targetDoc + "html", FileFormat.HTML);
    doc.saveToFile(targetDoc + "xlsx", FileFormat.XLSX);
    doc.saveToFile(targetDoc + "xps", FileFormat.XPS);
    doc.saveToFile(targetDoc + "svg", FileFormat.SVG);

    BufferedImage image;
    File fileSrc = new File(srcPdf);
    String fileName = FileUtils.getFileName(fileSrc);
    String path = fileSrc.getParent();
    for (int i = 0; i < doc.getPages().getCount(); ++i) {
      image = doc.saveAsImage(i);
      File file = new File(String.format("%s%s%s-%d.png", path, File.separator, fileName, i));
      try {
        ImageIO.write(image, "png", file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // 加密
    PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;
    String openPwd = "123456", permissionPwd = "demo";
    EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);
    doc.getSecurity().encrypt(openPwd, permissionPwd, flags, keySize);
    doc.saveToFile("C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.encrypt.pdf");
    doc.close();

    // 解密
    doc.loadFromFile("C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.encrypt.pdf", openPwd);
    // 解除文档密码保护
    try {
      doc.getSecurity()
          .encrypt("", "", PdfPermissionsFlags.getDefaultPermissions(), keySize, permissionPwd);
      doc.saveToFile("C:\\Users\\Jackie\\Downloads\\dd\\DevOps流程建设图.decrypt.pdf");
    } catch (Exception e) {
      e.printStackTrace();
    }

    doc.close();
    System.out.println("end");
  }

  public static void main4(String[] args) {
    IRecognizeMainColor recognizeMainColor = new RecognizeMainColor();
    System.out.println("please input the image file:");
    String imgPath = new Scanner(System.in).nextLine();
    String res = recognizeMainColor.run(imgPath);
    System.out.println(res);

    System.out.println("end.");
  }
}
